<?php 
using ('Origami_XML_Reader');

class News_Application_Profile
{
    var $id;
    var $path;
    function News_Application_Profile($id)
    {
        $this->id   = $id;   
        $this->path = "{$this->id}.xml"; 
    }
    
    function GetBookmarks ()
    {  
        $dom = $this->_getDom(); 
        $ret = array();  
        foreach ($dom->selectNodes('//bookmark') as $bookmark)
        {
            $ret[] = 'data/' . $this->id . '/' . $bookmark->get_attribute ('group') . '.' . $bookmark->get_attribute ('key');
        } 
        return $ret;
    }
    
    function Save ($settings)
    {  
        $dir   = 'data/' . $this->id;
        if (!is_dir ($dir)) mkdir ($dir);
        
        $xml   = array();
        $ids   = $settings['msgid'];
        $names = $settings['msgsubject'];
        $group = $settings['group'];
        
        $node  = '<article id="%s"><![CDATA[%s]]></article>';
        $head  = '<bookmark name="' . $settings['group'] . '">';
        $key   = $ids[0];
        $path  = $dir . '/' . $settings['group'] . '.' . $key;
        $db    = $this->_getDom();
        
        
        echo 'Saving ' . sizeof ($names) . ' articles to ' . $path;
        
        for ($x=0;$x<sizeof($ids);$x++)
        {
            $xml[] = sprintf($node, $ids[$x], $names[$x]);
        }
        
        $mark  = $db->selectSingleNode ("//bookmark[@key = '{$key}'][@group = '{$group}']");
        if (!$mark)
        {
            $root = $db->documentElement();
            $mark = $db->createElement ('bookmark');
            $mark->set_attribute ('key', $key);
            $mark->set_attribute ('group', $settings['group']);
            $root->append_child ($mark);
            $db->Save ($this->path);
        }
        
        
        $reader = new Origami_XML_Reader();
        $reader->OpenMem ($head . implode ('', $xml) . '</bookmark>');
        $reader->Save ($path);
   #     exit;
        
    }
    
    function _getDom ()
    {  
        $reader = new Origami_XML_Reader();
        if (file_exists($this->path))
        { 
            $reader->Open ($this->path);
        }
        else
        { 
            $reader->OpenMem ('<profile/>');
        }
        return $reader;
    }
    
    function getGroups()
    {
        $dom = $this->_getDom(); 
        $ret = array();  
        foreach ($dom->selectNodes('//group') as $group)
        {
            $ret[] = $group->get_attribute ('name');
        }
      #  print_r ($ret);
        return $ret;
    }
    
    function Subscribed ($groupName)
    {
        $dom = $this->_getDom(); 
        $old = $dom->selectSingleNode ("//group[@name='{$groupName}']");  
        return $old;
    }
    
    function Subscribe ($groupName)
    {
        $dom = $this->_getDom();
        $old = $dom->selectSingleNode ("//group[@name='{$groupName}']");  
        if ($old)
        {
        echo 'removing ' . $groupName;
            $old->unlink_node();
            $dom->Save($this->path);
            return;
        }
        echo 'subscribing to ' . $groupName;
        $top = $dom->documentElement();
        $new = $dom->createElement ('group');
        $new->set_attribute ('name', $groupName);
        $top->append_child ($new);
        $dom->Save($this->path);
        
        redirect (str_replace('/subscribe/yes', '',  $_SERVER['SCRIPT_URL']));
    }
}
